<template>
  <!-- 添加退货  -->
  <div class="contractInfo box">
    <el-card
      shadow="hover"
      :header="titleMap[mode]"
      class="item-background"
      style="width: 100%"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="addForm"
        label-width="auto"
        label-position="right"
      >
        <el-form-item label="标题" prop="title" style="width: 100%">
          <el-input
            v-model="form.title"
            clearable
            placeholder="请输入标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="退货订单" request>
          <el-button type="primary" plain @click="selectPurchaseOrder">
            选择订单</el-button
          >
        </el-form-item>

        <el-form-item label="待退货" prop="adminId" style="width: 100%">
          <el-table
            ref="table"
            :data="goodsList"
            placeholder="暂无数据"
            stripe
            border
          >
            <el-table-column label="物品名称" prop="productName">
            </el-table-column>
            <el-table-column label="规格型号" prop="specificationName">
            </el-table-column>
            <!-- <el-table-column label="单价(元)" prop="price" >
            </el-table-column> -->
            <el-table-column label="采购数量" prop="financeBuyNum">
            </el-table-column>
            <el-table-column label="采购单位" prop="unitName">
            </el-table-column>
            <el-table-column label="总价格(元)" prop="productBuyPrice">
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item label="备注" style="width: 100%">
          <el-input
            type="textarea"
            v-model="form.remark"
            rows="3"
            clearable
            placeholder="请输入备注"
          ></el-input>
        </el-form-item>
      </el-form>

      <!-- 审核流程项 -->
      <check-form-item
        ref="checkFormItem"
        @setCheckConfig="setCheckConfig"
        :field="this.form"
        :checkType="checkType"
        v-show="$route.query.mode == 'add'"
      ></check-form-item>

      <div class="button">
        <el-button type="primary" @click="save" :loading="isSaveing"
          >保 存</el-button
        >
        <el-button @click="cencel">取 消</el-button>
      </div>
    </el-card>

    <!-- 选择需要退回的采购单 -->
    <select-purchase-order
      ref="selectPurchaseOrder"
      @setOrder="setOrder"
    ></select-purchase-order>
  </div>
</template>
<script>
import selectPurchaseOrder from '../../components/selectPurchaseOrder'
import checkFormItem from '@/components/checkProcess/checkFormItem.vue'
export default {
  name: 'addPurchase',
  data () {
    return {
      checkType: 24,//工作流的类型
      mode: this.$route.query.mode,
      titleMap: {
        add: '添加',
        edit: '编辑',
      },
      isSaveing: false,
      attrObj: null,
      form: {
        adminId: '',
        id: '',
        title: '',
        remark: '',//备注
        exempt: '',//是否免审
        workflowJsonList: []
      },
      userInfo: {},
      goodsList: [],
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
      }
    }
  },
  components: {
    selectPurchaseOrder,
    checkFormItem

  },

  mounted () {
    this.userInfo = this.$TOOL.data.get('USER_INFO')
    this.form.adminId = this.userInfo.userId
    if (this.$route.query.mode == 'edit') this.getDetail()

  },
  methods: {
    // 审批流配置
    setCheckConfig (list, exempt) {
      this.form.workflowJsonList = list
      this.form.exempt = exempt
    },

    // 获取采购单详情
    async getDetail () {
      let param = {
        limit: 1000,
        page: 1,
        id: this.$route.query.id,
      }
      let { data } = await this.$API.purchase.getPurchaseList.get(param)
      this.form = data[0]
      this.goodsList = data[0].procurementProductList

    },

    //选择需要退货的采购单
    selectPurchaseOrder () {
      this.$refs.selectPurchaseOrder.open()
    },

    setOrder (data) {
      this.goodsList = data.procurementProductList
      this.form.id = data.id
    },

    // 添加
    save () {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          if (this.goodsList.length == 0) {
            this.$message.error("请选者需要退货的采购单")
            return
          }
          this.isSaveing = true
          let res = await this.$API.purchase.addReturnGoods.post(this.form)
          this.isSaveing = false
          if (res.code == 200) {
            this.closeSelectedTag(this.$route)
            this.$router.push({
              path: '/goods/returnGoods'
            })
            this.$message.success("操作成功")
          }

        }
      })
    },

    cencel () {
      this.closeSelectedTag(this.$route)
      this.$router.push({
        path: '/goods/returnGoods'
      })
    },
    //关闭tag
    closeSelectedTag (tag) {
      this.$store.commit("removeViewTags", tag)
      this.$store.commit("removeIframeList", tag)
      this.$store.commit("removeKeepLive", tag.name)
    },

  }
}
</script>
<style lang="scss" scoped>
.box {
  margin: 10px;
}
.el-form {
  // width: 100%;
  padding: 30px;
  background: #fff;
}

.button {
  margin-top: 50px;
}

.pass {
  :deep(.el-radio__input.is-checked + .el-radio__label) {
    color: #409eff !important;
  }

  :deep(.el-radio__input.is-checked) {
    .el-radio__inner {
      background: #409eff !important;
      border-color: #409eff !important;
    }
  }
}

.refuse {
  :deep(.el-radio__input.is-checked + .el-radio__label) {
    color: #f56c6c !important;
  }

  :deep(.el-radio__input.is-checked) {
    .el-radio__inner {
      background: #f56c6c !important;
      border-color: #f56c6c !important;
    }
  }
}
</style>